<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>采购申请表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">申请编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="requestnumber" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">申请人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="proposer" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <br>
                        <div class="layui-inline">
                            <label class="layui-form-label">日期范围</label>
                            <div class="layui-input-inline">
                                <input type="text"  name="searchDate" class="layui-input" id="test6" placeholder="请点击">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 新增采购申请 </button>
<!--
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
-->
            </div>
        </script>

        <script type="text/html" id="toolbarDemo3">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加物料 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">查看详情</a>
<!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"></a>-->
        </script>

        <script type="text/html" id="currentTableBar2">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //日期范围渲染
        laydate.render({
            elem: '#test6'
            ,range: true
        });

    });
</script>
<script>
    layui.use(['jquery','form', 'table','laydate'], function () {
        //使用jquery
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;


        //表格渲染
        table.render({
            elem: '#currentTableId',//表格的ID
            url: '/purchaserequest',
            toolbar: '#toolbarDemo',
            //头部工具栏右侧图标
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',//标题
                layEvent: 'LAYTABLE_TIPS',//事件名
                icon: 'layui-icon-tips'//图标类名
            }],
            cols: [[//表头
                /*
                * type:设置列类型
                * field:设定字段名
                * title:设定标题名
                * sort:是否允许排序
                * toolbar:绑定工具条模板。可在每行对应的列中出现一些自定义的操作性按钮
                * */
                {type: "checkbox", width: 50},//设置列类型
                {field: 'purId', width: 50, title: '编号'},
                {field: 'requestnumber', width: 200, title: '申请编号'},
                {field: 'requesttime', width: 300, title: '申请时间',
                    templet:'<div>{{ layui.util.toDateString(d.requesttime, "yyyy-MM-dd") }}</div>', sort: true},
                {field: 'proposer', width: 200, title: '申请人'},
                {field: 'applystatus', width: 200, title: '当前状态', sort: true,
                    templet: '<div>{{d.applicationRecord.applystatus}}</div>',
                    templet: function(d) {
                        var t = d.applicationRecord.applystatus;
                        if(t==0){
                            return '审核中'
                        }else if(t==1){
                            return '审核通过'
                        }
                    }},
                /*{field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'sign', title: '签名', minWidth: 150},
                {field: 'score', width: 80, title: '评分', sort: true},
                {field: 'classify', width: 80, title: '职业'},
                {field: 'wealth', width: 135, title: '财富', sort: true},*/
                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            //隐藏编号列
            done: function () {
                 $("[data-field='purId']").css('display','none');
             },

            limits: [5, 10, 15, 20, 25, 30],
            limit: 5,
            page: true,//开启分页
            skin: 'line',
        });
        // 解决隐藏列后排序后数据错误问题
        table.on('sort(currentTableId)', function (obj) {
            table.reload('currentTableId', {
                initSort: obj
            });
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    requestnumber: data.field.requestnumber,
                    proposer: data.field.proposer,
                    searchDate : data.field.searchDate

                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听添加采购申请表
         */
        table.on('toolbar(currentTableFilter)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../store/add-purchsererequest.html',
                });
                //弹出层过大被遮挡的解决方法
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            // else if (obj.event === 'delete') {  // 监听删除操作
            //     var checkStatus = table.checkStatus('currentTableId')
            //         , data = checkStatus.data;
            //     layer.alert(JSON.stringify(data));
            // }
        });



        //监听表格复选框选择
        // table.on('checkbox(currentTableFilter2)', function (obj) {
        //     //console.log(obj)
        // });

        //tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            alert(data.purId)
            //把传过来的采购单编号存到session中
            layui.sessionData('requestnumber',{key:'data',value:data.requestnumber});

            if(obj.event === 'edit'){
                var index = layer.open({
                    type: 1,
                    content: " <table class=\"layui-hide\" id=\"RequestUnitTableId\" lay-filter=\"currentTableFilter2\"></table>",
                    offset: 'lt'
                });
            }


            //表格渲染
            table.render({
                elem: '#RequestUnitTableId',
                url: '/purchaseitem?purId='+data.purId,
                toolbar: '#toolbarDemo3',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '物料详情',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                   // {type: "checkbox", width: 50},
                    {field: 'puiId', width: 50, title: '序号'},
                    {field: 'requestnumber', width: 200, title: '采购申请编号',templet: '<div>{{d.purchaserequest.requestnumber}}</div>' },
                    {field: 'maename', width: 180, title: '物料名称',templet: '<div>{{d.material.maename}}</div>'},
                    {field: 'model', width: 180, title: '规格型号', templet: '<div>{{d.material.model}}</div>'},
                    {field: 'texture', width: 180, title: '材质',templet: '<div>{{d.material.texture}}</div>'},
                    {field: 'unit', title: '计件单位', minWidth: 150,templet: '<div>{{d.material.unit}}</div>',sort: true},
                    {field: 'purchaseamount', width: 80, title: '数量',templet: '<div>{{d.purchaseamount}}</div>'},
                    {title: '操作', minWidth: 100, toolbar: '#currentTableBar2', align: "center"}

                ]],
                //隐藏编号列
                done: function () {
                    $("[data-field='puiId']").css('display','none');
                },
                limits: [4, 8, 16, 24],
                limit: 4,
                page: true,
                skin: 'line',

            });

            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
         /*   else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();//删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);//向服务端发送删除指令
                });
            }*/
        });
        table.on('tool(currentTableFilter2)', function (obj) {
            var data1 = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                     obj.del();//删除对应行（tr）的DOM结构，并更新缓存
                    // // alert(index);
                     layer.close(index);//
                    $.ajax({
                        //type:"post",
                        url:"/deletepurchaseitem",
                        data:{
                            puiId:data1.puiId,
                        },
                        success:function(data){
                            var msg = data.msg;
                            layer.open({
                                title:['确认消息'],
                                content: msg,
                                btn: ['确定'],
                                //控制点击弹层外区域关闭。
                                shadeClose: true,
                                //按钮1的回调
                                yes: function(index, layero){
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                }
                            })
                        }
                    });
                });
            }

        });

        /**
         * toolbar监听添加物料
         */
        table.on('toolbar(currentTableFilter2)', function (obj) {

            //取出session里面的值
            var requestnumberChild = layui.sessionData('requestnumber').data;

            //alert(requestnumberChild);
            // var checkStatus = table.checkStatus('RequestUnitTableId')
            // //获取到当前选中对象的采购项编号
            // var data1 =JSON.stringify(checkStatus.data[0].purchaserequest.requestnumber);
            // //去除"
            // var data2 = data1.replace("\"","")
            if (obj.event === 'add') {  // 监听添加操作

                var index = layer.open({
                    title: '添加物料',
                    type: 2,
                    shade: 0.2,//遮罩，透明度
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '../store/add-material.html?requestnumber='+requestnumberChild
                });
                //防止弹出层过大被遮挡
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }

        });
    });
</script>

</body>
</html>